package com.zzty.dao;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.zzty.entity.Library;

public class LibraryDao {
    public List<Library> selectBook() throws SQLException {
		@SuppressWarnings("unused")
		List<Library> list = new ArrayList<Library>();
		String resource = "Configuration.xml";	
		
        Reader reader = null;	
        
        try {
			 reader =Resources.getResourceAsReader("Configuration.xml");	

		} catch (IOException e) {
			e.printStackTrace();
		}
        
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);	
        SqlSession session = sessionFactory.openSession();	
        String statement = "com.zzty.selectBook";
        list=session.selectList(statement); 
		return list;
	}
	
	public boolean addBook(Library book) {
		@SuppressWarnings("unused")
		String resource = "Configuration.xml";	
        Reader reader = null;	
        
        try {
			 reader =Resources.getResourceAsReader("Configuration.xml");	

		} catch (IOException e) {
			e.printStackTrace();
		}
        
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);	
        SqlSession session = sessionFactory.openSession();	
        String statement = "com.zzty.addBook";
		boolean flag = false;
			int count=session.insert(statement,book);
			
			if(count>0){
				flag=true;
			}
		session.commit();
		return flag;
	}
	
	public boolean deleteBook(int id) {
		@SuppressWarnings("unused")
		String resource = "Configuration.xml";	
        Reader reader = null;	
        
        try {
			 reader =Resources.getResourceAsReader("Configuration.xml");	

		} catch (IOException e) {
			e.printStackTrace();
		}
        
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);	
        SqlSession session = sessionFactory.openSession();	
        String statement = "com.zzty.deleteBook";
		boolean flag = false;
			int count=session.insert(statement,id);
			
			if(count>0){
				flag=true;
			}
		session.commit();
		return flag;
	}
	
	public boolean updateBook(int id) {
		@SuppressWarnings("unused")
		String resource = "Configuration.xml";	
		Reader reader = null;	
		
		try {
			reader =Resources.getResourceAsReader("Configuration.xml");	
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);	
		SqlSession session = sessionFactory.openSession();	
		String statement = "com.zzty.updateBook";
		boolean flag = false;
		int count=session.insert(statement,id);
		
		if(count>0){
			flag=true;
		}
		session.commit();
		return flag;
	}
}
